package dbutils02;

import org.apache.commons.dbutils.QueryRunner;
import org.junit.Before;
import org.junit.Test;

/**
 * QueryRunner类：
 *      QueryRunner(DataSource ds)
 *          Constructor for QueryRunner that takes a DataSource to use.
 *      int update(String sql, Object... params)
 *          执行增删改查操作
 *          这个方法不用传入一个连接，是因为在使用QueryRunner构造方法时会传入一个数据源
 *          在执行该方法时会自动从这个数据源中获取一个连接
 */
public class DBUtilsDemo02 {
    private QueryRunner qr;

    @Before
    public void initQueryRunner(){
        //先初始化QueryRunner对象
        qr = new QueryRunner(JDBCUtils.getDataSource());
    }
    @Test
    public void testInsert() throws Exception {
        //1.利用连接池创建QueryRunner对象
        //QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());

        //2.利用QueryRunner类中的方法执行INSERT操作
        int i = qr.update("INSERT INTO product VALUES (?,?,?,?)", 14, "三星4k液晶电视", 8999, 4);

        //3.打印返回值
        System.out.println(i);
    }
    @Test
    public void testUpdate()throws Exception {
        int i = qr.update("UPDATE product SET price= price+3000 WHERE pid = ?",
                            14);
        System.out.println(i);
    }
    @Test
    public void testDelete() throws Exception {
        int i = qr.update("DELETE FROM product WHERE pid = ?", 14);
        System.out.println(i);
    }
}
